<template>
  <el-dialog :visible.sync="isShow" title="分配角色" @close="closeEvent">
    <el-checkbox-group v-model="checkValue">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysRole } from '@/api/setting.js'
import { sysUser } from '@/api/user.js'
import { sysUserAssignRolesPut } from '@/api/employees.js'
export default {
  data() {
    return {
      isShow: false,
      checkValue: [],
      list: [],
      id: '' // 当前项的id
    }
  },
  async created() {
    const res = await sysRole({ page: 1, pageSize: 1000 })
    this.list = res.data.rows
    console.log('角色列表数据', res)
  },
  methods: {
    async getRoleIds(id) {
      // 保存当前项的id
      this.id = id
      const res = await sysUser(id)
      this.checkValue = res.data.roleIds
      console.log('获取角色信息', res)
    },
    // 关闭弹框时触发
    closeEvent() {
      // 关闭弹框时清除勾选项
      this.checkValue = []
      //  关闭弹框
      this.isShow = false
    },
    // 点击提交按钮
    async submit() {
      await sysUserAssignRolesPut(this.id, this.checkValue)
      this.$message.success('分配角色成功')
      this.closeEvent()
    }
  }
}
</script>

<style></style>
